package com.binvshe.binvshe.account.regist.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.frameworkbase.customviews.IconTextArrowEditText;
import com.pachong.android.frameworkbase.customviews.edittext.CaptchaEditText;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegistCaptchaFragment extends BaseFragment {

    public static final String SMS_KEY = "6e38dd04a439";
    public static final String SMS_SECRET = "e1d1a2b7ba21a5eeae341b5d73e541a6";
    public static final String SMS_CHINA = "86";

    @Bind(R.id.caetCaptcha)
    CaptchaEditText mCaetCaptcha;
    @Bind(R.id.btnNext)
    Button mBtnNext;
    public static final String KEY_PHONE = "key_phone";
    private static final int CATPCHA_TYPE_REGISTER = 1;
    @Bind(R.id.etCaptchaa)
    EditText mEtCaptcha;


    String mPhone;

    public static RegistCaptchaFragment newInstance(String phone) {
        Bundle args = new Bundle();
        RegistCaptchaFragment fragment = new RegistCaptchaFragment();
        args.putString(KEY_PHONE, phone);
        fragment.setArguments(args);
        return fragment;
    }

    public RegistCaptchaFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
//        initMobSdk();
        initToolBar();
        mPhone = getArguments().getString(KEY_PHONE);
        mCaetCaptcha.setChackableimpl(new CaptchaEtListener());
        return view;
    }

   /* private void initMobSdk() {
        SMSSDK.initSDK(getActivity(), SMS_KEY, SMS_SECRET);
        SmsEvenHandler eh = new SmsEvenHandler();
        SMSSDK.registerEventHandler(eh);
    }*/

    private void initToolBar() {
        showToolbar();
        getCustomToolbar().setTitle("获取短信验证码");
        getCustomToolbar().enableBackButton().setOnClickListener(new BackBtnListener());
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.frag_reg_captcha, null);
    }

    private String getCaptcha() {
        return mEtCaptcha.getText().toString().trim();
    }

    @OnClick(R.id.btnNext)
    public void onClick() {
        Log.e("输入+++",getCaptcha().length()+"");
        if (TextUtils.isEmpty(getCaptcha())) {
            FragmentUtils.showWarnDialog("请输入验证码", getFragmentManager());
            return;
        }
//        SMSSDK.submitVerificationCode(SMS_CHINA, mPhone, getCaptcha());

        MyHttpRequest request = new MyHttpRequest(getContext());
        String UrlCheckCatpcha = String.format(UrlCenter.CHECK_CAPTCHA, mPhone, CATPCHA_TYPE_REGISTER, getCaptcha());
        Log.e("onClick: ", "UrlCheckCatpcha :" + UrlCheckCatpcha);
        request.get(UrlCheckCatpcha, null, new DataRequestListener<Object>(Object.class) {

            @Override
            public void onSuccess(Object o) {
                FragmentUtils.addFragment(getFragmentManager(), R.id.registContainer, RegistPwdFragment.newInstance(mPhone, getCaptcha()));
            }

            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                EasyToast.showToast(getContext(), strMsg);
            }
        });
    }

    private class CaptchaEtListener implements CaptchaEditText.SendCheckable {

        @Override
        public void sendCheckCode(TextView btnCount) {
//            SMSSDK.getVerificationCode(SMS_CHINA, mPhone);

            MyHttpRequest request = new MyHttpRequest(getContext());
            JSONObject jobj = new JSONObject();
            String UrlCaptcha = String.format(UrlCenter.CAPTCHA, mPhone, CATPCHA_TYPE_REGISTER);
            try {
                jobj.put(ParamKey.PHONE, mPhone);
                jobj.put(ParamKey.TYPE, CATPCHA_TYPE_REGISTER);
                request.post(UrlCaptcha, jobj, new DataRequestListener<Object>(Object.class) {

                    @Override
                    public void onSuccess(Object o) {
                        EasyToast.showToast(getContext(), "发送验证码成功!");
                    }

                    @Override
                    public void onStart(Context context, String url) {
                        enableProgress(false);
                        super.onStart(context, url);
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {
                        EasyToast.showToast(getContext() , strMsg);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private class BackBtnListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            FragmentUtils.backStack(getActivity());
        }
    }

   /* public class SmsEvenHandler extends EventHandler {

        @Override
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    FragmentUtils.addFragment(getFragmentManager(), R.id.registContainer, RegistPwdFragment.newInstance(mPhone, getCaptcha()));
                }
            } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                //获取验证码成功
                Toast.makeText(getActivity(), "验证码发送成功", Toast.LENGTH_SHORT).show();

            } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                //返回支持发送验证码的国家列表
            } else {
                FragmentUtils.showWarnDialog("验证码错误", getFragmentManager());
            }
        }
    }*/
}
